package com.hunan.api.catv.action.index;

import com.hunan.api.catv.action.BaseAction;
import com.hunan.api.catv.common.Api;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.Result;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import com.hunan.api.catv.service.biz.ding.DingBiz;
import com.hunan.api.catv.service.biz.homeRecommend.HomeRecommendBiz;
import com.hunan.api.catv.service.biz.mix.MixRecommendBiz;
import com.hunan.api.catv.service.biz.mq.MqBiz;
import com.hunan.api.catv.utils.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping(Api.index)
@Validated
public class IndexAction extends BaseAction {


    @Autowired
    CacheBiz cacheBiz;

    @Autowired
    DingBiz dingBiz;

    @Autowired
    HomeRecommendBiz homeRecommendBiz;

    @Autowired
    MixRecommendBiz recommendBiz;

    @Autowired
    MqBiz mqBiz;

    @GetMapping("/info")
    public Result info(String key, HttpServletRequest request) {
        //todo ios暂不发版邀请活动
        if ("4".equals(request.getHeader("c")) && "app_member_invitation".equals(key) && Integer.parseInt(request.getHeader("v")) <= 104) {
            return buildOK("", ConstantsMsg.admin_select_fail);
        }
        return buildOK(GsonUtils.fromJson(cacheBiz.apiDictCacheV2(key, request), Object.class), "");
    }

    /**
     * 新版本首页栏目列表更新
     */
    @GetMapping("/newInfo/{key}")
    public Result newsInfo(@PathVariable("key") String key, HttpServletRequest request) {
        //todo ios暂不发版邀请活动
        if ("4".equals(request.getHeader("c")) && "app_member_invitation".equals(key) && Integer.parseInt(request.getHeader("v")) <= 104) {
            return buildOK("", ConstantsMsg.admin_select_fail);
        }
        return buildOK(GsonUtils.fromJson(cacheBiz.apiDictCacheV3(key, request), Object.class), "");
    }

    /**
     * 首页精选列表
     *
     * @param pageNum
     * @return
     */
    @GetMapping("/list")
    public Result list(int pageNum, HttpServletRequest request) {
        return buildOK(recommendBiz.page(pageNum, getPageSize(request)), "");
//        return buildOK(cacheBiz.recommendListPage(pageNum, getPageSize(request)),"");
    }

    /**
     * 首页精选置顶列表
     *
     * @param request
     * @return
     */
    @GetMapping("/topList")
    public Result topList(HttpServletRequest request) {
        return buildOK(recommendBiz.topList(), "");
//        return buildOK(cacheBiz.recommendTopList(),"");
    }


    /**
     * 升级
     *
     * @param request
     * @return
     */
    @GetMapping("/upgrade")
    public Result upgrade(HttpServletRequest request, @RequestParam(required = false) String cName) {
        log.info("收到打开app事件。。。。。。。。{}", getMemberIdV2(request));
        mqBiz.push(MqConstants.OPEN_APP_EVENT_EXCHANGE, MqConstants.OPEN_APP_EVENT_ROUTINGKEY, getMemberIdV2(request));
        return buildOK(cacheBiz.upgradeCache(getChannel(request), getClientVersion(request), cName), "");

    }

    @PostMapping("/androidNotify")
    public Result androidNotify(HttpServletRequest request, @RequestParam(required = false) String text) {
        return buildResult(dingBiz.dingNotify(text, getChannel(request)));
    }

    @GetMapping("/home-recommend")
    public Result recommendInfo(HttpServletRequest request) {
        return buildOK(cacheBiz.homeRecommend("key"), "");

    }
}